package com.jblv.frame.common.kafka.bean;

import kafka.serializer.Decoder;
import kafka.utils.VerifiableProperties;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;

/**
 * @author happyonion.yangtao
 * @version 1.0
 * @title ObjectDecoder
 * @projectName jblv-frame
 * @packageName com.jblv.web.kafka
 * @description TODO
 * @date Create at 2020/4/17 13:59
 * @since jdk1.8
 */
public class ObjectDecoder implements Decoder<MqBaseMessage> {
    public ObjectDecoder() {
    }

    public ObjectDecoder(VerifiableProperties properties) {
    }

    @Override
    public MqBaseMessage fromBytes(byte[] bytes) {
        MqBaseMessage obj = null;
        ByteArrayInputStream bi = null;
        ObjectInputStream oi = null;

        try {
            bi = new ByteArrayInputStream(bytes);
            oi = new ObjectInputStream(bi);
            obj = (MqBaseMessage)oi.readObject();
            bi.close();
            oi.close();
        } catch (Exception var18) {
            System.out.println("translation" + var18.getMessage());
            var18.printStackTrace();
        } finally {
            if (bi != null) {
                try {
                    bi.close();
                } catch (IOException var17) {
                    var17.printStackTrace();
                }

                bi = null;
            }

            if (oi != null) {
                try {
                    oi.close();
                } catch (IOException var16) {
                    var16.printStackTrace();
                }

                oi = null;
            }

        }

        return obj;
    }
}
